package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import model.Hospede;

public class HospedeDao {
    GenericDao gDao = new GenericDao();
    Connection c = GenericDao.getConnection();

    public boolean insereHospede(Hospede hospede){ 
        boolean inserido = false;
        try{
            PreparedStatement ps = c.prepareStatement("insert into t_hospede values (?,?,?,?,?,?,?,?,?)");
            ps.setString(1, hospede.getNome());
            ps.setString(2, hospede.getCpf());
            ps.setString(3, hospede.getRg());
            ps.setString(4, hospede.getEndereco());
            ps.setString(5, hospede.getTelefone());
            ps.setString(6, hospede.getEmail());
            ps.setBoolean(7, hospede.isDebito());
            ps.setBoolean(8, hospede.isQuartoLocado());
            ps.setBoolean(9, hospede.isQuartoReservado());
            ps.execute();
            ps.close();
            inserido = true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return inserido;
    }

    public Hospede consultaHospede(String cpf){ // alterei o retorno dos dados
        Hospede hospede = new Hospede();
        ResultSet rs = null;
        try{
            PreparedStatement ps = c.prepareStatement("select * from t_hospede where nr_cpf = ?");
            ps.setString(1, cpf);
            ps.execute();
            ps.close();
            rs = ps.getResultSet();
            if (rs.next()){
                hospede.setNome(rs.getString(2));
                hospede.setRg(rs.getString(4));
                hospede.setEndereco(rs.getString(5));
                hospede.setTelefone(rs.getString(6));
                hospede.setEmail(rs.getString(7));
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return hospede;
    }
    
    public List<Hospede> listaCompletaHospede(){
    	List<Hospede> listaCompleta = new ArrayList<>();
    	ResultSet rs = null;
    	try{
    		PreparedStatement ps = c.prepareStatement("select * from t_hospede order by nm_hospede");
    		rs = ps.executeQuery();
    		while(rs.next()){
    			Hospede hospede = new Hospede();
    			hospede.setID(rs.getInt(1));
                hospede.setNome(rs.getString(2));
                hospede.setCpf(rs.getString(3));
                hospede.setRg(rs.getString(4));
                hospede.setEndereco(rs.getString(5));
                hospede.setTelefone(rs.getString(6));
                hospede.setEmail(rs.getString(7));
                listaCompleta.add(hospede);
    		}
    		ps.close();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
		return listaCompleta;
    }


    public boolean atualizaHospede(Hospede hospede){
        boolean editado = false;
        try{
            PreparedStatement ps = c.prepareStatement(
            		"update t_hospede set nm_hospede = ?, nr_rg = ?, nm_endereco = ?, " +
            		"nr_telefone = ?, nm_email = ?  where nr_cpf = ?");

            ps.setString(1, hospede.getNome());
            ps.setString(2, hospede.getRg());
            ps.setString(3, hospede.getEndereco());
            ps.setString(4, hospede.getTelefone());
            ps.setString(5, hospede.getEmail());
            ps.setString(6, hospede.getCpf());
            ps.execute();
            ps.close();
            editado = true;
        } catch(Exception e){
            e.printStackTrace();
        }
        return editado;
    }

    public boolean deletaHospede(String cpf){
        boolean deletado = false;
        try{
            PreparedStatement ps = c.prepareStatement("delete t_hospede where nr_cpf = ?");
            ps.setString(1, cpf);
            ps.execute();
            ps.close();
            deletado = true;
        } catch(Exception e){
                e.printStackTrace();
        }
        return deletado;
    }
}
